/* 
 * ------------------------------------------------------------------------
 * This is the android graphics  class that implements basic game interface 
 * graphics.  
 * 2011, by Wu Mu Xian
 * --------------------------------------------------------------------
*/

package mygame.myandroidgameframework;

import java.io.IOException;
import java.io.InputStream;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Paint.Style;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import mygame.basicgameframework.Graphics;
import mygame.basicgameframework.Pixelmap;

public class MyAndroidGraphics implements Graphics {
	
	AssetManager assets;
	Bitmap frameBuffer;
	Canvas canvas;
	Paint paint;
	Typeface font;
	Rect srcRect = new Rect();
	Rect dstRect = new Rect();

	public MyAndroidGraphics(AssetManager assets, Bitmap frameBuffer) { 
        this.assets = assets; 
        this.frameBuffer = frameBuffer; 
        this.canvas = new Canvas(frameBuffer); 
        this.paint = new Paint(); 
    }
	
	// load a bitmap from an asset file using the pixel map format
	public Pixelmap newPixelmap(String fileName, PixmapFormat format) {
		Config config = null;
		if (format == PixmapFormat.RGB565)
			config = Config.RGB_565;
		else if (format == PixmapFormat.ARGB4444)
			config = Config.ARGB_4444;
		else
			config = Config.ARGB_8888;
		
		Options options = new Options(); 
        options.inPreferredConfig = config; 
 
        InputStream in = null; 
        Bitmap bitmap = null; 
        try { 
            in = assets.open(fileName); 
            bitmap = BitmapFactory.decodeStream (in); 
            if (bitmap == null) 
                throw new RuntimeException("Couldn't load bitmap from asset '" 
                        + fileName + "'"); 
        } catch (IOException e) { 
            throw new RuntimeException("Couldn't load bitmap from asset '" 
                    + fileName + "'"); 
        } finally { 
            if (in != null) { 
                try { 
                    in.close(); 
                } catch (IOException e) { 
                } 
            } 
        } 
 
        // the bitmap factory might decide to ignore our desired color
        // format, so we have to check afterward what it decoded the 
        // image to.
        if (bitmap.getConfig() == Config.RGB_565 ) 
            format = PixmapFormat.RGB565 ; 
        else if (bitmap.getConfig() == Config.ARGB_4444 ) 
            format = PixmapFormat.ARGB4444 ; 
        else 
            format = PixmapFormat.ARGB8888 ; 
 
        return new MyAndroidPixelmap(bitmap, format);
	}

	// clear the canvas with a color
	public void clear(int color) {
		canvas.drawRGB((color & 0xff0000) >> 16, (color & 0xff00) >> 8, 
				(color & 0xff));
	}
	
	//draw a text
	public void drawText(String string, int position, int fontsize) { 
        font = Typeface.createFromAsset(assets,"solo5.ttf");
        paint.setColor(Color.WHITE); 
        paint.setTypeface(font);
        paint.setTextSize(fontsize); 
        paint.setTextAlign(Paint.Align.CENTER); 
        canvas.drawText(string, canvas.getWidth() / 2, position, paint);
	}

	// draw a pixel at a point
	public void drawPixel(int x, int y, int color) {
		paint.setColor(color);
		canvas.drawPoint(x, y, paint);
	}

	// draw a line between two points
	public void drawLine(int x, int y, int x2, int y2, int color) {
		paint.setColor(color);
		canvas.drawLine(x, y, x2, y2, paint);
	}

	// draw a rectangle start a (x,y) with width and height
	public void drawRect(int x, int y, int width, int height, int color) {
		paint.setColor(color);
		paint.setStyle(Style.FILL);
		canvas.drawRect(x, y, x + width -1, y + height -1, paint);
	}

	// draw a pixel map
	public void drawPixelmap(Pixelmap pixmap, int x, int y) {
		canvas.drawBitmap(((MyAndroidPixelmap)pixmap).bitmap, x, y, null);
	}

	// get the width of the frame buffer
	public int getWidth() {
		return frameBuffer.getWidth();
	}

	// get the height of the frame buffer
	public int getHeight() {
		return frameBuffer.getHeight();
	}

	// draw a certain part of the pixel map
	public void drawPixelmap(Pixelmap pixmap, int x, int y, int srcX, int srcY,
			int srcWidth, int srcHeight) {
		srcRect.left = srcX; 
        srcRect.top = srcY; 
        srcRect.right = srcX + srcWidth - 1; 
        srcRect.bottom = srcY + srcHeight - 1; 
 
        dstRect.left = x; 
        dstRect.top = y; 
        dstRect.right = x + srcWidth - 1; 
        dstRect.bottom = y + srcHeight - 1; 
 
        canvas.drawBitmap(((MyAndroidPixelmap) pixmap).bitmap, srcRect, dstRect, 
                null);	
	}
	
	// draw pixel map to a certain area
	public void drawPixelmap(Pixelmap pixmap, int x, int y, int disWidth, int disHeight) {
		dstRect.left = x;
		dstRect.top = y;
		dstRect.right = x + disWidth -1;
		dstRect.bottom = y + disHeight -1;
		
		canvas.drawBitmap(((MyAndroidPixelmap) pixmap).bitmap, null, dstRect, 
                null);
	}
}
